package com.example.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.entity.SysMenu;
import com.example.auth.entity.dto.*;
import com.example.auth.entity.vo.SysRoleQueryVO;
import com.example.auth.entity.vo.SysUserQueryVO;
import com.example.auth.service.SysRoleService;
import com.example.auth.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author: LiYang
 * @create: 2023-07-11 16:12
 * @Description:
 * @Version 1.0
 **/
@RestController
@RequestMapping("system/role")
@Validated
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @PostMapping("getSysRolePageList/{pageNum}/{pageSize}")
    @ApiOperation("分页查询角色")
    public Result<Page<SysRoleQueryVO>> getSysRolePageList(@PathVariable Long pageNum,
                                     @PathVariable Long pageSize,
                                     @RequestBody SysRoleQueryDTO sysRoleQueryDTO){
        Page<SysRoleQueryVO> sysRoleQueryVOPage = sysRoleService.selectRolePage(pageNum, pageSize, sysRoleQueryDTO);
        return Result.success(sysRoleQueryVOPage);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("insertRole")
    @ApiOperation("新增角色")
    public Result<Void> insertRole(@RequestBody @Valid SysRoleCreateDTO sysRoleCreateDTO){
        sysRoleService.insertRole(sysRoleCreateDTO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("getRoleById/{id}")
    @ApiOperation("通过id查询角色")
    public Result<SysRoleQueryVO> getRoleById(@PathVariable Long id){
        SysRoleQueryVO sysRoleQueryVO = sysRoleService.getRoleById(id);
        return Result.success(sysRoleQueryVO);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PostMapping("updateRole")
    @ApiOperation("修改角色信息")
    public Result<Void> updateRole(@RequestBody @Valid SysRoleEditDTO sysRoleEditDTO){
        sysRoleService.updateRole(sysRoleEditDTO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("removeRoleById/{id}")
    @ApiOperation("删除角色")
    public Result<Void> removeRoleById(@PathVariable Long id){
        sysRoleService.removeRoleById(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("removeBatch")
    @ApiOperation("批量删除角色")
    public Result<Void> removeBatch(@RequestBody List<Long> idList) {
        sysRoleService.removeBatch(idList);
        return Result.success();
    }

    @GetMapping("updateRoleStatus/{roleId}/{status}")
    @ApiOperation("更改角色状态")
    public Result<Void> updateRoleStatus(@PathVariable Long roleId,
                                   @PathVariable Long status){
        sysRoleService.updateRoleStatus(roleId,status);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @GetMapping("toAssign/{roleId}")
    @ApiOperation("根据角色id获取菜单数据")
    public Result<List<SysMenu>> toAssign(@PathVariable Long roleId){
        List<SysMenu> sysMenuList = sysRoleService.findSysMenuByRoleId(roleId);
        return Result.success(sysMenuList);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @PostMapping("doAssign")
    @ApiOperation("分配菜单")
    public Result<Void> doAssign(@RequestBody AssignMenuDTO assignMenuDTO){
        sysRoleService.doAssign(assignMenuDTO);
        return Result.success();
    }

}
